var params = {};

$(function(){
	params.currentPage = 1;
	loadAllData();
});

function loadAllData(){
	loadAllUser();
	loadAllGroup();
	loadAllRole();
	//绑定删除事件
	$('#allGroup').on('click','.fa-trash',function(){remove(this);});
	$('#allRole').on('click','.fa-trash',function(){remove(this);});
}

function allowDrop(ev)
{
	ev.preventDefault();
}

function drag(ev)
{
	ev.dataTransfer.setData("Text",ev.target.id);
}

function drop(ev)
{
	var data = ev.dataTransfer.getData("Text");
	var target = ev.target;

	var addUrl = '';
	var params = {};
	
	var exist = false;
	if($(target).hasClass('isGroup'))
	{
		//判断是否已存在
		$('#allGroup .groupContent').each(function(index){
			if($('#'+data).attr('id')==$(this).attr('id')){
				exist = true;
				return false;
			}
		});
		
		if(!exist){
			var appendStr = '<div id="'+$('#'+data).attr('id')+'" class="groupContent isGroup">'
			+ '<i class="fa fa-user isGroup" aria-hidden="true"></i>'
			+ $('#'+data).text()
			+'<i class="fa fa-trash isGroup" aria-hidden="true"></i></div>';
			$('#allGroup .opencontent').append(appendStr);
			//用户添加到组
			addUrl = webUrl + 'sysGroup/addSysUserToGroup';
			params.userid = $('#'+data).attr('id');
			params.groupid = $('#allGroup .opencontent').attr('id');
		}
	}else if($(target).hasClass('isRole')){
		//判断是否已存在
		$('#allRole .roleContent').each(function(index){
			if($('#'+data).attr('id')==$(this).attr('id')){
				exist = true;
				return false;
			}
		});
		
		if(!exist){
			var appendStr = '<div id="'+$('#'+data).attr('id')+'" class="roleContent isRole">';
			if($('#'+data).hasClass('groupContent')){
				//用户组添加到角色
				appendStr += '<i class="fa fa-users isRole"></i>';
				appendStr += $('#'+data).text()+'<i class="fa fa-trash isRole groupRole"></i></div>';
				addUrl = webUrl+'sysRole/addGroupToSysRole';
				params.groupid = $('#'+data).attr('id');
			}else{
				//用户添加到角色
				appendStr += '<i class="fa fa-user isRole"></i>';
				appendStr += $('#'+data).text()+'<i class="fa fa-trash isRole userRole"></i></div>';
				addUrl = webUrl+'sysRole/addUserToSysRole';
				params.userid = $('#'+data).attr('id');
			}
			params.roleid = $('#allRole .opencontent').attr('id');
			
			$('#allRole .opencontent').append(appendStr);
		}
	}
	
	if(addUrl!='')
	{
		$.ajax({
			url:addUrl,
			type:'POST',
			data:params
		});
	}
}

function remove(obj){
	var params = {};
	var removeUrl = '',id='';
	if($(obj).hasClass('isGroup')){
		removeUrl = webUrl + 'sysGroup/deleteUserFormGroup';
		params.userid = $(obj).parent().attr('id');
		var groupid = $('#allGroup .opencontent').attr('id');
		params.groupid = groupid;
		id = groupid;
	}else if($(obj).hasClass('isRole')){
		
		var roleid = $('#allRole .opencontent').attr('id');
		
		if($(obj).hasClass('userRole'))
		{
			params.userid = $(obj).parent().attr('id');
			removeUrl = webUrl + 'sysRole/deleteUserFormRole';
		}else if($(obj).hasClass('groupRole')){
			params.groupid = $(obj).parent().attr('id');
			removeUrl = webUrl + 'sysRole/deleteGroupFormRole';
		}
		id = roleid;
		params.roleid = roleid;
	}
	
	if(removeUrl!='')
	{
		$.ajax({
			url:removeUrl,
			type:'POST',
			data:params
		}).done(function(data){
			$(obj).parent().remove();
		});
	}
}

function loadAllUser(){
	$('#allUser').empty();
	params.pageSize = 0;
	params.status='Y';
	$.ajax({
		url:webUrl+'sysUser/pageSysUser',
		type:'POST',
		data:params
	}).done(function(data){
		var result = data.bo.list;
		for(var i=0;i<result.length;i++)
		{
			$('#allUser').append('<div id="'+result[i].id+'" draggable="true" ondragstart="drag(event)"><i class="fa fa-user" aria-hidden="true"></i>'+result[i].username+'</div>');
		}
	});
}

function loadAllGroup(){
	$('#allGroup').empty();
	params.pageSize = 0;
	params.status='Y';
	$.ajax({
		url:webUrl+'sysGroup/pageSysUserGroup',
		type:'POST',
		data:params
	}).done(function(data){
		var result = data.bo.list;
		for(var i=0;i<result.length;i++)
		{
			$('#allGroup').append('<div id="'+result[i].id+'" class="groupContent"  draggable="true" ondragstart="drag(event)"><i class="fa fa-users" aria-hidden="true"></i>'+result[i].groupname+'</div>');
		}
		
		$('.groupContent').on('dblclick',function(){
			$('#allGroup').empty();
			var appendstr = '<div id="'+$(this).attr('id')+'" class="opencontent isGroup" ondrop="drop(event)" ondragover="allowDrop(event)"><div class="groupup isGroup"><i class="fa fa-reply isGroup" aria-hidden="true"></i></div>'
				+'<i class="fa fa-users isGroup" aria-hidden="true"></i>'+$(this).text()+'</div>';
			$('#allGroup').append(appendstr);
			loadGroupUser($(this).attr('id'));
			$('.groupup').on('dblclick',function(){
				loadAllGroup();
			});
		});
	});
}

function loadAllRole(){
	$('#allRole').empty();
	params.pageSize = 0;
	params.status='Y';
	$.ajax({
		url:webUrl+'sysRole/pageSysRole',
		type:'POST',
		data:params
	}).done(function(data){
		var result = data.bo.list;
		for(var i=0;i<result.length;i++)
		{
			$('#allRole').append('<div id="'+result[i].id+'" class="roleContent"><i class="fa fa-user-secret" aria-hidden="true"></i>'+result[i].rolename+'</div>');
		}

		$('.roleContent').on('dblclick',function(){
			$('#allRole').empty();
			var roleId = $(this).attr('id');
			var appendstr = '<div id="'+roleId+'" class="opencontent isRole" ondrop="drop(event)" ondragover="allowDrop(event)"><div class="roleup isRole"><i class="fa fa-reply isRole" aria-hidden="true"></i></div>'
				+'<i class="fa fa-user-secret isRole" aria-hidden="true"></i>'+$(this).text()+'</div>';
			$('#allRole').append(appendstr);
			loadRoleUser(roleId);
			loadRoleGroup(roleId);
			$('.roleup').on('dblclick',function(){
				loadAllRole();
			});
		});
	});
}

function onUserInput(obj){
	params.username = $(obj).val();
	loadAllUser();
}

function onGroupInput(obj){
	params.groupname = $(obj).val();
	loadAllGroup();
}

function onRoleInput(obj){
	params.rolename = $(obj).val();
	loadAllRole();
}

function loadGroupUser(id){
	$.ajax({
		url:webUrl+'sysGroup/getUserByGroup',
		type:'POST',
		data:{groupid:id}
	}).done(function(data){
		var result = data.bo;
		for(var i=0;i<result.length;i++){
			var appendStr = '<div id="'+result[i].userid+'" class="groupContent isGroup">'
			+ '<i class="fa fa-user isGroup" aria-hidden="true"></i>'
			+ result[i].username
			+'<i class="fa fa-trash isGroup" aria-hidden="true"></i></div>';
			$('#allGroup .opencontent').append(appendStr);
		}
	});
}

function loadRoleUser(id){
	$.ajax({
		url:webUrl+'sysRole/getUserByRole',
		type:'POST',
		data:{roleid:id}
	}).done(function(data){
		var result = data.bo;
		for(var i=0;i<result.length;i++){
			var appendStr = '<div id="'+result[i].userid+'" class="roleContent isRole">'
			+ '<i class="fa fa-user isRole" aria-hidden="true"></i>'
			+ result[i].username
			+'<i class="fa fa-trash isRole userRole" aria-hidden="true"></i></div>';
			$('#allRole .opencontent').append(appendStr);
		}
	});
}

function loadRoleGroup(id){
	$.ajax({
		url:webUrl+'sysRole/getGroupByRole',
		type:'POST',
		data:{roleid:id}
	}).done(function(data){
		var result = data.bo;
		for(var i=0;i<result.length;i++){
			var appendStr = '<div id="'+result[i].groupid+'" class="roleContent isRole">'
			+ '<i class="fa fa-users isRole" aria-hidden="true"></i>'
			+ result[i].groupname
			+'<i class="fa fa-trash isRole groupRole" aria-hidden="true"></i></div>';
			$('#allRole .opencontent').append(appendStr);
		}
	});
}



